package com.me.mysqladmin.controller;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.me.mysqladmin.entity.TableCreate;
import com.me.mysqladmin.entity.TableName;
import com.me.mysqladmin.entity.TableStatus;
import com.me.mysqladmin.service.TableService;
import com.me.mysqladmin.util.EasyUIFormat;

@Controller
@RequestMapping("/tableStatus")
public class TableStatusController extends BaseController {
	
	// 注入业务组件
	@Resource
	private TableService tableService;
	
	/**
	 * 跳转到主页
	 */
	@RequestMapping("/index.do")
	public String toIndex(){
		return "main/tableStatus";
	}
	
	/**
	 * 跳转到创建表页
	 */
	@RequestMapping("/createTablePage.do")
	public String toCreateTable(){
		return "extra/createTable";
	}
	
	/**
	 * 查询指定数据库下的所有表信息
	 * @param databaseName 数据库名
	 */
	@RequestMapping("/queryPage.do")
	@ResponseBody
	public Map<String, Object> queryAll(String databaseName){
		if(databaseName == null || "".equals(databaseName.trim())){
			return EasyUIFormat.parseGrid(Collections.emptyList(), LinkedHashMap.class);
		}
		List<Map<String, Object>> tables = tableService.showTables(databaseName);
		return EasyUIFormat.parseGrid(tables, LinkedHashMap.class);
	}
	
	/**
	 * 删除一张表
	 */
	@RequestMapping("/dropTable.do")
	@ResponseBody
	public Map<String, Object> dropTable(TableStatus tableStatus){
		try {
			LOG.info("--------删除表：" + tableStatus + "--------");
			Integer line = tableService.deleteTable(tableStatus);
			return messageSuccess(line);
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "删除表：[" + tableStatus.getTable_name() + "]失败！");
		}
	}
	
	/**
	 * 重命名一张表
	 */
	@RequestMapping("/renameTable.do")
	@ResponseBody
	public Map<String, Object> renameTable(TableName tableName){
		try {
			LOG.info("--------重命名表：" + tableName + "--------");
			tableService.renameTable(tableName);
			return messageSuccess();
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "重命名失败！");
		}
	}
	
	/**
	 * 创建一张表
	 */
	@RequestMapping("/createTable.do")
	@ResponseBody
	public Map<String, Object> createTable(String jsonFields, TableStatus tableStatus){
		try {
			LOG.info("--------创建表：" + tableStatus.getDatabaseName() + "." + tableStatus.getTable_name() + "--------");
			LOG.info("--------字段信息：" + jsonFields + "--------");
			TableCreate tableCreate = new TableCreate();
			// 获取行记录
			ObjectMapper objectMapper = new ObjectMapper();
			tableCreate = objectMapper.readValue("{\"fields\":" + jsonFields +"}", tableCreate.getClass());
			// 设置创建位置
			tableCreate.setLocation(tableStatus.getDatabaseName() + "." + tableStatus.getTable_name());
			// 设置存储引擎
			tableCreate.setEngine(tableStatus.getEngine());
			// 设置表注释
			tableCreate.setTable_comment(tableStatus.getTable_comment());
			LOG.info("--------TableCreate:" + tableCreate + "--------");
			tableService.createTable(tableCreate);
			return messageSuccess();
		} catch (Exception e) {
			return messageFailure(e.getMessage(), "建表失败！");
		}
	}
}
